package nc.bs.voucher.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import nc.bs.voucher.itf.IVoucherDetailPrintService;
import nc.vo.pub.BusinessException;
import nccloud.nc.ui.trade.excelimport.io.XLSFileUtil;
import nccloud.ui.trade.excelimport.parser.IFileParserConstants;

public class VoucherDetailPrintServiceImpl implements IVoucherDetailPrintService{

	@Override
	public InputStream VoucherDetailPrint(List<ArrayList<Object>> listvalues) throws Exception {
		SXSSFWorkbook wb = new SXSSFWorkbook(1000);
		Sheet sheet = wb.createSheet();
		CellStyle headStyle = wb.createCellStyle();
		headStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
		headStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
		
		CellStyle leftTextStyle = wb.createCellStyle();
		DataFormat format = wb.createDataFormat();
		leftTextStyle.setDataFormat(format.getFormat("@"));
		int dataRow = 1;//数据开始行是1，如果有表头信息需要加2
		for(int i = 0 ; i < listvalues.size() ; i++) {
			Row row = sheet.createRow(i + dataRow);
			for(int j = 0 ; j< listvalues.get(i).size();j++) {
				Cell cell = row.createCell(j);
				String value = listvalues.get(i).get(j).toString();
				cell.setCellStyle(leftTextStyle);
				cell.setCellValue(value);
				
			}
		}
		File excelfile = File.createTempFile("initExport", IFileParserConstants.XLSX_SUFFIX);
		excelfile.deleteOnExit();
		XLSFileUtil.write(wb, excelfile);
		return  new FileInputStream(excelfile);
	}

}
